package com.atguigu.tingshu.user.strategy.impl;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserPaidAlbum;
import com.atguigu.tingshu.user.mapper.UserPaidAlbumMapper;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author atguigu-mqx
 * @ClassName VipStrategy
 * @description: TODO
 * @date 2025年03月10日
 * @version: 1.0
 */
/*
<bean id="1001" class="com.atguigu.tingshu.user.strategy.impl.AlbumStrategy"></bean>
spring 源码底层存储bean时，使用map来存储的；map.put("1001",AlbumStrategy);
 */
@Service("1001")
@Slf4j
public class AlbumStrategy implements ItemTypeStrategy {

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;

    @Override
    public int savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //  声明一个变量
        int result = 0;
        //  根据itemType 类型判断;
        if (SystemConstant.ORDER_ITEM_TYPE_ALBUM.equals(userPaidRecordVo.getItemType())) {
            //  购买了专辑 user_paid_album;
            //  查询当前记录表中已有数据;
            UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getUserId, userPaidRecordVo.getUserId()).eq(UserPaidAlbum::getAlbumId, userPaidRecordVo.getItemIdList().get(0)));
            //  判断
            if (null != userPaidAlbum) {
                //  没有插入数据
                return result;
            }
            //  创建一个对象
            userPaidAlbum = new UserPaidAlbum();
            //  赋值;
            userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
            userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
            userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
            //  保存数据;
            return result += userPaidAlbumMapper.insert(userPaidAlbum);
        }
        //  默认返回
        return result;
    }
}
